﻿using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using admin.WebUI2.Models;
using admin.Bussniess;
using admin.Model;
using Microsoft.AspNetCore.Http;

namespace admin.WebUI2.Controllers
{
    public class HomeController : Controller
    {


        public IActionResult Index(string name,string password)
        {
            //连数据库验证

            //HttpContext.Session.SetString("code", "myname");
            Administrator admin = null;
            //第一次启动或刷新后的情况，无seesion取值
            if (name == null)
            {
                return View("Index");
            }
            if (name!=null)
            {
                var service = new ManagerService();
                //验证返回接收一个错误类型或model，
                admin = service.SignVerify(name, password);
            }
            //登录失败的情况，提示错误信息，保持登录界面
            if (admin == null)
            {
                ViewData["error"] = 1;
                return View("Index");
            }
            //登录成功的情况，拿到admin对象即验证通过
            else
            {
                HttpContext.Response.Cookies.Append("adminName", admin.Name);
                HttpContext.Session.SetString("adminName", admin.Name);
                HttpContext.Session.SetString("adminId", admin.Id);
                ViewData["admin"] = admin;
                ViewData["name"] = admin.Name;
                
                return View("Home");
            }
        }

        public IActionResult Home()
        {
            //if(ViewData["name"]==null) 表示未登录，非法操作
            var adminName="";
            HttpContext.Request.Cookies.TryGetValue("adminName",out adminName);
            ViewData["name"] = adminName;
            return View();
        }

        public IActionResult BookManage()
        {
            var service = new ManagerService();
            var bookList=service.GetAllBook();
            ViewData["bookList"] = bookList;
            ViewData["name"] = HttpContext.Session.GetString("adminName");
            return View("BookManage",bookList);
        }

        public IActionResult Information()
        {
            var service = new ManagerService();
            var admin = service.QueryAdmin(HttpContext.Session.GetString("adminId"));
            //if(admin==null) 
            ViewData["name"] = HttpContext.Session.GetString("adminName");
            return View(admin);
        }

        public IActionResult UserManage()
        {
            var service = new ManagerService();
            var customers = service.GetALlCustomer();
            ViewData["name"] = HttpContext.Session.GetString("adminName");  // Cookies.TryGetValue("myCookie", out myCookie);
            return View(customers);
        }

        public IActionResult AuthorManage()
        {
            var service = new ManagerService();
            var authors = service.GetAllAuthor();
            ViewData["name"] = HttpContext.Session.GetString("adminName");
            return View(authors);
        }

        public IActionResult OrderManage()
        {
            var service = new ManagerService();
            var orders = service.GetAllOrder();
            ViewData["name"] = HttpContext.Session.GetString("adminName");
            return View(orders);
        }

        public IActionResult SaveInformation(string adminID, string adminName, string adminPhone)
        {

            var service = new ManagerService();
            var admin = service.UpdateAdmin(adminID, adminName, adminPhone);
            if (admin != null)
                ViewData["changeInformation"] = true;
            else
                ViewData["changeInformation"] = false;
            HttpContext.Response.Cookies.Append("adminName", admin.Name);
            HttpContext.Session.SetString("adminName", admin.Name);
            ViewData["name"] = admin.Name;
            return View("Information",admin);
        }

        public IActionResult ChangePassword(string oldPassword,string newPassword,string renewPassword)
        {
            var service = new ManagerService();
            var oldadmin = service.QueryAdmin(HttpContext.Session.GetString("adminId"));
            if (oldadmin.Password.Equals(oldPassword))
            {
                if (newPassword.Equals(renewPassword))
                {
                    oldadmin.Password = newPassword;
                    var admin = service.UpdateAdmin(oldadmin);
                    if ( admin!= null)
                    {
                        HttpContext.Response.Cookies.Append("adminName", admin.Name);
                        HttpContext.Session.SetString("adminName", admin.Name);
                        ViewData["name"] = admin.Name;
                        ViewData["changePassword"] = true;
                        return View("Information",admin);
                    }
                }
            }
            ViewData["name"] = oldadmin.Name;
            ViewData["changePassword"] = false;
            return View("Information",oldadmin);
        }


        //未实现
        public IActionResult ResetPassword()
        {
            var service = new ManagerService();
            var oldadmin = service.QueryAdmin(HttpContext.Session.GetString("adminId"));

            oldadmin.Password = "123456";
            var admin = service.UpdateAdmin(oldadmin);
            if (admin != null)
            {
                ViewData["ResetPassword"] = true;
                return View("Information", admin);
            }
            ViewData["ResetPassword"] = false;
            return View("Information", oldadmin);
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
